package com.nbgroup.service;

import com.nbgroup.dao.IchnographyRepositoty;
import com.nbgroup.entity.Ichnography;
import com.nbgroup.utils.QueryUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * 平面图Service
 * 2018年8月16日, AM 10:56:53
 *
 * @author 姜文
 */
@Service
@Transactional
public class IchnographyService {

    @Autowired
    private IchnographyRepositoty ichnographyRepositoty;
    @Autowired
    private QueryUtil queryUtil;

    /**
     * 查询 全部or指定层 火灾馆平面图
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public List<Ichnography> findAllForIchnography(Ichnography ichnography){
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("SELECT * FROM `ichnography` WHERE sts = '1' ");
        if (ichnography.getFloorId() != null) {
            stringBuffer.append(" and floor_id = '"+ichnography.getFloorId()+"'");
        }
        return queryUtil.query(stringBuffer.toString(), Ichnography.class);
    }

    /**
     * 火灾馆平面图保存
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public int saveIchnography(Ichnography ichnography){
        try{
            ichnography.setSts("1");
            ichnographyRepositoty.save(ichnography);
            return 1;
        }catch(Exception e){
            return -1;
        }
    }

    /**
     * 火灾馆平面图修改
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public int updateIchnography(Ichnography ichnography){
        try{
            ichnography.setSts("1");
            ichnographyRepositoty.saveAndFlush(ichnography);
            return 1;
        }catch(Exception e){
            return -1;
        }
    }

    /**
     * 火灾馆平面图findOne
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public Ichnography findOneForIchnography(Integer ichnographyId){
        String sql = "SELECT * FROM `ichnography` WHERE ichnography_id = '"+ichnographyId+"'";
        List<Ichnography> ichnographyList = queryUtil.query(sql, Ichnography.class);
        if(ichnographyList != null){
            return ichnographyList.get(0);
        }else{
            return null;
        }
    }

    /**
     * 火灾馆平面图删除--逻辑
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public int deleteIchnography(Integer ichnographyId){
        try{
            Ichnography ichnography = this.findOneForIchnography(ichnographyId);
            ichnography.setSts("0");
            ichnographyRepositoty.saveAndFlush(ichnography);
            return 1;
        }catch(Exception e){
            return -1;
        }
    }
}
